/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package elektra;

import java.util.logging.Level;
import java.util.logging.Logger;
import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Message;

/**
 *
 * @author adee
 */
public class CommandProcessor implements  MessageListener {
    private  Logger LOG = Logger.getLogger(CommandProcessor.class.getName());

    private Elektra elektra;

    public CommandProcessor(Elektra elektra) {
        this.elektra = elektra;
    }
    
    
    
    
    @Override
    public void processMessage(Chat chat, Message msg) {
     
           String backMessage ="";
           String body = msg.getBody().trim();
           RawCommandExecutor executor = new RawCommandExecutor(body);
           
           
        try{
           executor.execute();
           backMessage = executor.getOutput();
        }  catch (Exception ex){
            
            LOG.info("Failed to execute command:"+body);
           /*Not a valid command */
            backMessage = elektra.getRespond(body);
            LOG.info("Get respond from Elektra");
            
        } 
        try {
            
            chat.sendMessage(backMessage);
        } catch (XMPPException ex) {
            LOG.log(Level.SEVERE,"nem sikerült elküldeni az outputot", ex);
        }
    
    }
    
}
